package com.vvlive.caibao.smartcity.ui;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.utils.ScreenUtil;
import com.vvlive.caibao.smartcity.view.CommonActionBar;
import com.vvlive.caibao.smartcity.view.RoundImageView;

import org.kymjs.kjframe.ui.BindView;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/7/15.
 * <p/>
 * 编辑个人资料页面
 */
public class EditPersonalInfoActivity extends BaseActivity {

    private final static int TAKE_PHOTO_REQUEST_CODE = 1;
    private final static int PICK_ALBUM_REQUEST_CODE = 2;

    private final static String PHOTO_RESULT_KEY = "data";

    @BindView(id = R.id.details_avatar, click = true)
    RoundImageView avatar;

    /**
     * 显示头像来源选择的PopupWindow
     */
    private PopupWindow window;
    /**
     * PopupWindow的上TextView的点击事件
     */
    private WindowClickListener windowClickListener;
    /**
     * 新的头像路径
     */
    private String newAvatarPath;

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_edit_personal_info;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        CommonActionBar actionBar = getCommonActionBar();
        actionBar.setTitle("编辑资料");
        actionBar.setMoreVisible(false);
        windowClickListener = new WindowClickListener();
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()) {
            case R.id.details_avatar:
                // 点击头像，选择要修改的头像的来源
                showPickPhotoWindow();
                break;
        }
    }

    /**
     * 根据PopupWindow选择要修改的头像的来源
     */
    private void showPickPhotoWindow() {

        if (window == null) {
            View contentView = getLayoutInflater().inflate(R.layout.popupwindow_pick_photo_for_avatar, null);
            int width = (int) new ScreenUtil(this).getScaledSize(200);
            int height = ViewGroup.LayoutParams.WRAP_CONTENT;
            // set the PopupWindow's content, width, height and whether gets focus.
            window = new PopupWindow(contentView, width, height, true);
            window.setBackgroundDrawable(new BitmapDrawable());
            Button takePhoto = (Button) contentView.findViewById(R.id.pick_photo_take_photo);
            takePhoto.setOnClickListener(windowClickListener);
            Button fromAlbum = (Button) contentView.findViewById(R.id.pick_photo_from_album);
            fromAlbum.setOnClickListener(windowClickListener);
        }
        window.showAsDropDown(avatar);
    }

    private final class WindowClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.pick_photo_take_photo:    // 拍照，调用系统相机
                    Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(photoIntent, TAKE_PHOTO_REQUEST_CODE);
                    break;
                case R.id.pick_photo_from_album:    // 选择相册，调用系统相册
                    Intent albumIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(albumIntent, PICK_ALBUM_REQUEST_CODE);
                    break;
            }
            window.dismiss();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK) {

            if (requestCode == TAKE_PHOTO_REQUEST_CODE) {    // take the photo successfully

                Bundle bundle = data.getExtras();
                Bitmap bitmap = (Bitmap) bundle.get(PHOTO_RESULT_KEY);
                avatar.setImageBitmap(bitmap);


            } else if (requestCode == PICK_ALBUM_REQUEST_CODE && data != null) {   // get the photo of system album successfully

                Uri selectedImage = data.getData();
                String[] filePathColumns = {MediaStore.Images.Media.DATA};
                Cursor cursor = this.getContentResolver().query(selectedImage, filePathColumns, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumns[0]);
                newAvatarPath = cursor.getString(columnIndex);
                cursor.close();
                Bitmap bitmap = BitmapFactory.decodeFile(newAvatarPath);
                avatar.setImageBitmap(bitmap);

            }

        } else {

            toast("操作异常，请重试");

        }
    }
}
